var app = angular.module('webApp', ['ui.router', 'oc.lazyLoad', 'ngCookies','iService','iDirective','Filter']);
// 页面路由
app.config(['$controllerProvider','$stateProvider','$urlRouterProvider', '$locationProvider','$ocLazyLoadProvider','$httpProvider',
    function($controllerProvider,$stateProvider,$urlRouterProvider,$locationProvider,$ocLazyLoadProvider,$httpProvider) {
    //动态拆分控制器，指令，过滤器，工厂，服务
    app.register = {
        controller: $controllerProvider.register,
        /*directive: $compileProvider.directive,
        filter: $filterProvider.register,
        factory: $provide.factory,
        service: $provide.service,
        constant: $provide.constant*/
    };
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
    //路由状态重写
    var loadResolve = function (files,options) {
        var loadPlugin = {
            deps: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    files: files
                });
            }]
        }
        if(options){
            angular.forEach(options,function (value,key) {
                loadPlugin[key] = value;
            })
        }
        return loadPlugin;
    }
    //动态路由
    $stateProvider
    .state('home', {    //首页
        url:'/:name',
        templateUrl: __uri('/views/home.html'),
        controller:'homeCtrl',
        resolve : loadResolve([__uri('../css/home.less')])
    })/*.state('detail', {    //详情
        url:'/detail/:type/:code',
        templateUrl: 'views/detail.html',
        controller:'detailCtrl',
        resolve : loadResolve([__uri('../css/lists.less'),__uri('/src/js/ctrl/detail.js')])
    }).state('traderank', {    //行业排行榜
        url:'/traderank',
        templateUrl: 'views/trade-rank.html',
        controller:'traderankCtrl',
        resolve : loadResolve([__uri('../css/trade-rank.less')])
    }).state('listsall', {    //红黑榜-总榜
        url:'/listsall',
        templateUrl: 'views/lists-all.html',
        controller:'listsallCtrl',
        resolve : loadResolve([__uri('../css/lists.less')])
    }).state('liststrade', {    //红黑榜-行业榜
        url:'/liststrade/:name',
        templateUrl: 'views/lists-trade.html',
        controller:'liststradeCtrl',
        resolve : loadResolve([__uri('../css/lists.less')])
    })*/.state('tradeall', {    //行业-总榜
        url:'/tradeall/:name',
        templateUrl: __uri('/views/trade-all.html'),
        controller:'tradeallCtrl',
        resolve : loadResolve([__uri('../css/lists.less')])
    })/*.state('result', {    //筛选结果
        url:'/result',
        templateUrl: 'views/result.html',
        controller:'resultCtrl',
        resolve : loadResolve([__uri('../css/lists.less')])
    }).state('resulttxt', {    //搜索结果
        url:'/resulttxt',
        templateUrl: 'views/result-txt.html',
        controller:'resultCtrl',
        resolve : loadResolve([__uri('../css/lists.less')])
    })*/.state('gradeDetail', {    //搜索结果
        url:'/gradedetail/:name',
        templateUrl: __uri('/views/detail-grade.html'),
        controller:'gradedetailCtrl',
        resolve : loadResolve([__uri('../css/lists.less'),__uri('/src/js/ctrl/detail.js')])
    });
    $urlRouterProvider.otherwise('/recruit');
    // $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('');


}]);

// 在所有route跳转前进行登录检查，目标页面需要登录且用户未登录，则跳转到登录页面
app.run(['$rootScope', '$cookies', '$location', '$http', function($rootScope,$cookies,$location,$http) {
    /*$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
        //去除别的页面带的连接参数
        var searchs = $location.search();
        if(toState.name!='result' && JSON.stringify(searchs)!="{}"){
            $location.search({}).replace();
        }

        //微信分享
        if(toState.name!='detail'){
            $config.wxShare();
        }
    })*/

    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
        if(!$rootScope.config){
            $http.get($config.host+'?m=Evaluate&a=config&evaluate_flag='+toParams.name).success(function (json) {
                if(json.errcode==0){
                    $rootScope.config = json.data;
                    $config.wxShare({
                        title:'这些隐私政策“靠谱”么？'+json.data.type+'篇',
                        url : 'http://h5.oeeee.com/h5/v17/evaluation-new/#/'+toParams.name
                    });
                }
            })
        }
    })

}]);

app.controller('appCtrl',['$scope','$rootScope','$location', function ($scope,$rootScope,$location) {
    //跳转页面
    $scope.goPage = function (url) {
        $location.path(url);
    }
    
    //设置title
    $scope.update_title = function(title) {
        var body = document.getElementsByTagName('body')[0];
        document.title = title;
        var iframe = document.createElement("iframe");
        iframe.setAttribute("src", "/empty.png");

        iframe.addEventListener('load', function() {
            setTimeout(function() {
                iframe.removeEventListener('load');
                document.body.removeChild(iframe);
            }, 0);
        });
        document.body.appendChild(iframe);
    }
}]);
